
package com.thon.service.util.impl;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.thon.commons.utils.CollectionToCollection;
import com.thon.dao.util.HighCharts;
import com.thon.dao.util.HighCharts.Series;
import com.thon.entity.project.Data;
import com.thon.service.util.AnalysisResult;

/**
 * @file AnalysisResultImpl.java
 * @author manpiaoyi
 * @param <T>
 * @email manpiaoyi@126.com
 * @date 2014-11-9
 * @description 数据分析服务接口
 */
public class HighchartsAnalysisResultImpl implements AnalysisResult<Data,HighCharts>{

	private Date endDate;	
	private Map<Integer,String> map;
	private Integer method;
	private Integer strartToEndLength;
	private String[] xAxisCategories;
	private String[] xAxisCategoriesNum;
	private DateFormat df = null;
	private String[] xAxisNames;
	
	
	public HighchartsAnalysisResultImpl(Date startDate,Date endDate,Map<Integer,String> map,Integer method,Integer strartToEndLength)
	{
		this.endDate = endDate;
		this.map = map;
		this.method =method;
		this.strartToEndLength = strartToEndLength;
	}
	@Override
	public HighCharts writeAnalysisResult(List<Data> datum) {
		HighCharts highCharts  = new HighCharts();		
		produceXAxisCategories();
		if(datum!=null)
		produceHighCharts(datum,highCharts,strartToEndLength);		
		return highCharts;
	}	
	
	
	private void produceXAxisCategories() {
		
		xAxisCategories = new String[strartToEndLength];
		xAxisCategoriesNum = new String[strartToEndLength];
		Calendar calStart = Calendar.getInstance();
		calStart.setTime(endDate);		
		switch (method) {
		case Calendar.YEAR:
			df = new SimpleDateFormat("yyyy");
			xAxisNames = null;
			break;
		case Calendar.MONTH:
			df = new SimpleDateFormat("MM");
			xAxisNames = YEAR_MONTH;
			break;
		case Calendar.DAY_OF_MONTH:
			df = new SimpleDateFormat("dd");
			xAxisNames = MONTH_DAY;
			break;
		default:
			break;
		}
		for (int i = strartToEndLength - 1; i >= 0; i--) {

			xAxisCategories[i] = setXAxisName(df.format(calStart.getTime()));
			xAxisCategoriesNum[i] = df.format(calStart.getTime());
			calStart.add(method, -1);
		}
		
	}
	
	private String setXAxisName(String format) {
		if(xAxisNames!=null)
		{
			return  xAxisNames[Integer.parseInt(format)-1];
		}
		return format;
	}
	private void produceHighCharts(List<Data> datum,HighCharts highCharts,Integer xAxisLength)
	{		
		
		Double totle =0.0;
		Integer length = map.size();		
	
		Series[] series = new Series[length];
	
		int i=0;
		for (Map.Entry<Integer, String> entry : map.entrySet()) {
			series[i] = highCharts.new Series();
			series[i++].setName(entry.getValue());
	  }		
		
		
		Calendar cal = Calendar.getInstance();
		
		Double[][] seriesData = new Double[length][xAxisLength];				
		
		for (i = 0; i < seriesData.length; i++)
			 for (int j = 0; j < seriesData[i].length; j++)
			 {
				 seriesData[i][j]=0.0;
			 }
		
		
		
		for (Data data : datum) {
			
			cal.setTime(data.getCreatDate());			

			if (null != data.getType()) {
				int ind = CollectionToCollection.findIndex(map,data.getType().getId());
				int xAxis = CollectionToCollection.findIndex(xAxisCategoriesNum,df.format(cal.getTime()));
				if (ind != -1&&xAxis!=-1)
					seriesData[ind][xAxis] += data.getValue();

			}

			totle += data.getValue();
		}
		
		for (i = 0; i < length; i++) {
			series[i].setData(seriesData[i]);
		}
		
		highCharts.setxAxisCategories(xAxisCategories);
		highCharts.setSeries(series);
		highCharts.setTotle(totle);
		
	}
		


}